const CourseModel = require('../db/models/course');

class CourseService {
    //爬取课程
    async addCourseData (data) {
        const cid = data.cid;

        const result = await CourseModel.findOne({
            where: { cid }
        })

        if (result) {
            return await CourseModel.update(data, {
                where: { cid }
            })
        } else {
            return await CourseModel.create(data);
        }
    }

    //查询所有课程
    async getCourseData () {
        return await CourseModel.findAll({
            attributes: {
                exclude: ['posterUrl', 'description', 'createdAt', 'updatedAt']
            }
        });
    }

    //修改课程分类
    async changeField (cid, field) {
        const ret = await CourseModel.update({ field: field }, {
            where: { cid }
        })

        return ret[0];
    }

    //修改课程上下架状态
    async changeCourseStatus (id, status) {
        const ret = await CourseModel.update({ status }, {
            where: { cid: id }
        });

        return ret[0];
    }
}

module.exports = new CourseService();